home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Graphics / SMan / MorphOS / Source / Save.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-12  |  5.0 KB  |  191 lines

  1. #include    <clib/all_protos.h>
  2. #include    <exec/memory.h>
  3. #include    <iff/iff.h>
  4. #include    <iff/ilbm.h>
  5. #include    <iffp/packer.h>
  6.  
  7. /*
  8. #include <proto/all.h>
  9. #include <exec/memory.h>
  10. #include <iff/iff.h>
  11. #include <iff/ilbm.h>
  12. #include <iffp/packer.h>
  13. #include <stdio.h>
  14. */
  15.  
  16. extern WORD WWidth,WHeight,WDepth;
  17. extern struct Screen *WBScreen;
  18. extern struct BitMap *TmpBitMap;
  19. extern struct BitMap *NWBitMap;
  20. extern struct RastPort VTmpRast;
  21. extern struct RastPort *CWRast;
  22. extern struct RastPort NWRast;
  23. extern LONG BLeft,BTop;
  24. extern int WriteFile(char *,ULONG);
  25. extern UBYTE *GetColors(UBYTE *,ULONG);
  26. extern int OpenWrite(UBYTE *);
  27. extern void CloseWrite(void);
  28. extern void ChangeNW(void);
  29. extern void DisLine(struct RastPort *,char *,int);
  30. extern struct NewWindow nw;
  31. extern struct TextFont *CWFont;
  32. extern struct Window *IntWindow;
  33. extern struct GfxBase *GfxBase;
  34.  
  35. void Save(void)
  36. {
  37. ULONG    CTSize,CTCount,HeaderLength,BodyLength,ActualBody,LineLength;
  38. LONG    PixCnt;
  39. ULONG    *HdrSize,*HdrTop,*HdrBottom;
  40. UBYTE    *MyColors;
  41. ChunkHeader        *Chunk3D;
  42. BitMapHeader    *MyHeader;
  43.  
  44. typedef        struct    {
  45.     ID        ckID;
  46.     }    sChunk;
  47.  
  48. SHORT PlnCnt;
  49. UBYTE *OffPtr;
  50. struct Window *SavingWnd;
  51. UBYTE TmpArray[1600];
  52. sChunk    *ChunkID;
  53. char    *HdrBuffer,*BodyBuffer,*BytPtr;
  54.  
  55. ULONG    *ModBuf;
  56. USHORT    LineCount;
  57.     HdrBuffer = AllocMem(1200,MEMF_PUBLIC|MEMF_CLEAR);
  58.     if (!HdrBuffer) {
  59.         printf("Couldn't allocate memory for the header\n");
  60.         return;
  61.         }
  62.     Chunk3D = (ChunkHeader *) HdrBuffer;
  63.     Chunk3D -> ckID = FORM;
  64.     Chunk3D++;
  65.     ChunkID = (sChunk *) Chunk3D;
  66.     ChunkID -> ckID = ID_ILBM;
  67.     ChunkID++;
  68.     ChunkID -> ckID = ID_BMHD;
  69.     ChunkID++;
  70.     HdrSize = (ULONG *)ChunkID;
  71.     *HdrSize++ = 20;
  72.     MyHeader = (BitMapHeader *)HdrSize;
  73.     MyHeader -> w = WWidth;
  74.     MyHeader -> h = WHeight;
  75.     MyHeader -> compression = 1;
  76.     CTCount = WDepth;
  77.     MyHeader -> nPlanes = CTCount;
  78.     MyHeader -> xAspect = 10;
  79.     MyHeader -> yAspect = 10;
  80.     MyHeader -> pageWidth = WWidth;
  81.     MyHeader -> pageHeight = WHeight;
  82.     MyHeader++;
  83.     Chunk3D = (ChunkHeader *)MyHeader;
  84.     Chunk3D -> ckID = ID_CMAP;
  85.     CTSize = 1;
  86.     while (CTCount)    {
  87.         CTSize <<= 1;
  88.         --CTCount;
  89.         }
  90.     Chunk3D -> ckSize = 3*CTSize;
  91.     Chunk3D++;
  92.     MyColors = (UBYTE *)Chunk3D;
  93.     CTCount = WDepth;
  94.     MyColors = GetColors(MyColors,CTSize);
  95.     Chunk3D = (ChunkHeader *)MyColors;
  96.     Chunk3D -> ckID = ID_CAMG;
  97.     Chunk3D -> ckSize = 4;
  98.     Chunk3D++;
  99.     ModBuf = (ULONG *)Chunk3D;
  100.     *ModBuf++ = WBScreen->ViewPort.Modes;
  101.     Chunk3D = (ChunkHeader *)ModBuf;
  102.     Chunk3D -> ckID = ID_BODY;
  103.     CTCount = WWidth;
  104.     CTCount >>= 3;
  105.     CTCount = CTCount*WHeight;
  106.     CTCount = CTCount*WDepth;
  107.     TmpBitMap = (struct BitMap *)AllocBitMap((ULONG)WWidth,1,WDepth,BMF_CLEAR|BMF_DISPLAYABLE,WBScreen->RastPort.BitMap);
  108.     if (!TmpBitMap) {
  109.         printf("Couldn't allocate the 1st BitMap!\n");
  110.         if (HdrBuffer) FreeMem(HdrBuffer,1200);
  111.         }
  112.     InitRastPort(&VTmpRast);
  113.     VTmpRast.Layer = NULL;
  114.     VTmpRast.BitMap = TmpBitMap;
  115.     NWBitMap = (struct BitMap *)AllocBitMap((ULONG)WWidth,WHeight,WDepth,BMF_CLEAR,0);
  116.     if (!NWBitMap) {
  117.         printf("Couldn't allocate the 2nd BitMap!\n");
  118.         FreeBitMap(TmpBitMap);
  119.         if (HdrBuffer) FreeMem(HdrBuffer,1200);
  120.         }
  121.     InitRastPort(&NWRast);
  122.     NWRast.BitMap = NWBitMap;
  123.     CTCount = NWRast.BitMap->BytesPerRow;
  124.     CTCount = CTCount*WHeight;
  125.     CTCount = CTCount*WDepth;
  126.     BodyLength = CTCount;
  127.     BodyBuffer = AllocMem(BodyLength,MEMF_CLEAR);
  128.     if (!BodyBuffer) {
  129.         printf("Couldn't allocate memory for the image!\n");
  130.         FreeBitMap(NWBitMap);
  131.         FreeBitMap(TmpBitMap);
  132.         if (HdrBuffer) FreeMem(HdrBuffer,1200);
  133.         }
  134.     ChangeNW();
  135.     SavingWnd = OpenWindow(&nw);
  136.     SetFont(SavingWnd->RPort,CWFont);
  137.     BytPtr = BodyBuffer;
  138.     PlnCnt = 0;
  139.     LineCount = 0;
  140.     while (LineCount < WHeight)    {
  141.         ReadPixelLine8(CWRast,BLeft,LineCount+BTop,WWidth,&TmpArray[0],&VTmpRast);
  142.         WritePixelLine8(&NWRast,0,LineCount,WWidth,&TmpArray[0],&VTmpRast);
  143.         DisLine(SavingWnd->RPort,"Prepping Line ",WHeight - LineCount);
  144.         LineCount++;
  145.         }
  146.     LineCount = 0;
  147.     ActualBody = 0;
  148.     LineLength = NWRast.BitMap->BytesPerRow;
  149.     while (LineCount < WHeight)    {
  150.         PlnCnt = 0;
  151.         while (PlnCnt < WDepth) {
  152.             OffPtr = (UBYTE *)NWRast.BitMap->Planes[PlnCnt];
  153.             OffPtr = OffPtr + LineCount*LineLength;
  154.             PixCnt = 0;
  155.             PixCnt = PackRow((BYTE **)OffPtr,(BYTE **)BytPtr,(LONG)LineLength);
  156.             if (!PixCnt) printf("0 returned, and Body Size Current is %lx\n",(ULONG)ActualBody);
  157.             ActualBody = ActualBody + PixCnt;
  158.             PlnCnt++;
  159.             }
  160.         DisLine(SavingWnd->RPort,"Saving Line ",WHeight - LineCount);
  161.         Delay(1);
  162.         ++LineCount;
  163.         }
  164. /*    printf("The body length is %x\n",ActualBody);*/
  165.     if ((ActualBody/2)*2 != ActualBody) {
  166.         Chunk3D -> ckSize = ActualBody;
  167.         ActualBody++;
  168.             }
  169.     else Chunk3D -> ckSize = ActualBody;
  170.     Chunk3D++;
  171.     HdrTop = (ULONG *)Chunk3D;
  172.     HdrBottom = (ULONG *)HdrBuffer;
  173.     HeaderLength = HdrTop - HdrBottom;
  174.     HeaderLength <<= 2;     /*Get # bytes */
  175.     Chunk3D = (ChunkHeader *)HdrBuffer;
  176.     CTCount = ActualBody + HeaderLength;
  177.     Chunk3D -> ckSize = CTCount - 8;
  178.     Delay(50);
  179.     OpenWrite((UBYTE *)"Mand.iff");
  180.     WriteFile(HdrBuffer,HeaderLength);
  181.     WriteFile(BodyBuffer,ActualBody);
  182.     CloseWrite();
  183.     Delay(50);
  184.     if (SavingWnd) CloseWindow(SavingWnd);
  185.     if (BodyBuffer) FreeMem(BodyBuffer,BodyLength);
  186.     if (HdrBuffer) FreeMem(HdrBuffer,1200);
  187.     FreeBitMap(NWBitMap);
  188.     FreeBitMap(TmpBitMap);
  189. }
  190.  
  191.